package com.tencreat.service.pt.scheduler;


import com.tencreat.common.core.HttpResult;
import com.tencreat.common.utils.CronUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
//@Component
public class CommonScheduler {

/*    public static HttpResult<?> ptJob(Date date, String name, String group) throws SchedulerException {
        String cron = CronUtil.getCron(date);
        log.info("cron表达式:" + cron);

        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        //构建Job信息
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
        JobDetail jobDetail = JobBuilder
                .newJob(PtTheLotteryJob.class)
                .withIdentity(name, group)
                .storeDurably()
                .build();

        //填充Job默认执行器
        jobDetail.getJobDataMap().put("id", name);//拼团活动编号
//        jobDetail.getJobDataMap().put("ptGoodsName", "拼团商品名");//拼团商品名称

        CronTrigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity(name, group)
                .withSchedule(cronScheduleBuilder)
                .build();

        try {
            scheduler.scheduleJob(jobDetail, trigger);
            if (!scheduler.isStarted()) {
                scheduler.start();
            }
            log.info("拼团任务创建成功,cron表达式" + cron);
            return HttpResult.ok("拼团任务创建成功,cron表达式" + cron);
        } catch (SchedulerException e) {
            e.printStackTrace();
            log.error("拼团任务创建失败！" + e.getMessage());
            return HttpResult.error("拼团任务创建失败！" + e.getMessage());
        }
    }*/

    public static HttpResult<?> removeJob(String name, String group) throws SchedulerException {
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(name, group);
            // 停止触发器
            scheduler.pauseTrigger(triggerKey);
            // 移除触发器
            scheduler.unscheduleJob(triggerKey);
            // 删除任务
            scheduler.deleteJob(JobKey.jobKey(name, group));
            return HttpResult.ok("删除拼团任务成功");
        } catch (Exception e) {
            e.printStackTrace();
            return HttpResult.error("删除定时拼团任务失败！！请重试,错误信息为：" + e.getMessage());
        }
    }

    public static HttpResult<?> pauseJob(String name, String group) throws SchedulerException {
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        try {
            JobKey key = new JobKey(name, group);
            scheduler.pauseJob(key);
            return HttpResult.ok("定时任务停用成功");
        } catch (SchedulerException e) {
            e.printStackTrace();
            return HttpResult.error("定时任务停用失败！！请重试，错误信息为：" + e.getMessage());
        }
    }

    public static HttpResult<?> resumeJob(String name, String group) throws SchedulerException {
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        try {
            JobKey key = new JobKey(name, group);
            scheduler.resumeJob(key);
            return HttpResult.ok("定时任务启用成功");
        } catch (SchedulerException e) {
            e.printStackTrace();
            return HttpResult.error("定时任务启用失败！！错误信息为：" + e.getMessage());

        }
    }

/*    public static HttpResult<?> updateJob(Date date, String name, String group) {
        String cron = CronUtil.getCron(date);
        log.info("cron表达式:" + cron);
        TriggerKey triggerKey = TriggerKey.triggerKey(
                name, group);
        try {
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            if (trigger == null) {
                return ptJob(date, name, group);
            }
            String oldTime = trigger.getCronExpression();
            if (!oldTime.equalsIgnoreCase(cron)) {
                CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
                //按新的cron表达式重新构建trigger
                trigger = trigger.getTriggerBuilder().withIdentity(triggerKey)
                        .withSchedule(scheduleBuilder).build();
                //按新的trigger重新设置执行时间
                scheduler.rescheduleJob(triggerKey, trigger);
            }
            log.info("------------cron时间修改成功-------------");
            return HttpResult.ok("定时任务时间修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("------------定时任务时间修改失败-------------");
            return HttpResult.error("定时任务时间修改失败！！错误信息为：" + e.getMessage());
        }
    }*/
}
